package com.tronsis.distribution.activity.me.upgrade;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.ApplyDepositBean;
import com.tronsis.distribution.bean.QuitDepositBean;
import com.tronsis.distribution.bean.UserBean;
import com.tronsis.distribution.biz.factory.UpgradeBizFactory;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.distribution.util.StateUtil;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.MultiItemTypeAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DateUtil;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.util.ToastUtil;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * allen: 退押申请列表
 */
public class ReturnApplyListActivity extends ToolBarBaseActivity {

    @BindView(R.id.rv_return_apply)
    RecyclerView mRvReturn;
    @BindView(R.id.btn_apply)
    Button mBtnApply;


    private List<QuitDepositBean> mDataList = new ArrayList<>();
    private UserBean mLoginUserBean = UserBizFactory.createBiz().getLoginUser(this);
    private CommonAdapter<QuitDepositBean> adapter;
    private ApplyDepositBean applyDepositBean = new ApplyDepositBean();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_return_apply_list);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.apply));
        initRv();
        getQuitDeposit();
        httpGetUserInfo();
        getApplyDeposit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            getQuitDeposit();
        }
    }

    private void initRv() {
        adapter = new CommonAdapter<QuitDepositBean>(mContext, mDataList, R.layout.item_quit_apply) {
            @Override
            protected void convert(ViewHolder holder, QuitDepositBean quitDepositBean, int position) {
                holder.setText(R.id.tv_state, quitDepositBean.getDone() ? "已完成" : "待审核");
                if (quitDepositBean.getDone()) {
                    holder.setText(R.id.tv_time, DateUtil.getDateTimeString(quitDepositBean.getStateOn()));
                } else {
                    holder.setText(R.id.tv_time, DateUtil.getDateTimeString(quitDepositBean.getCreatedOn()));
                }
                holder.setText(R.id.tv_order_code, getString(R.string.format_upgrade_order_no, quitDepositBean.getCode()));
                holder.setText(R.id.tv_level, getString(R.string.format_upgrade_level, StateUtil.getLevelText(quitDepositBean.getLevel())));
                holder.setText(R.id.tv_money, getString(R.string.format_quit_money, getString(R.string.format_price, quitDepositBean.getDeposit())));
            }


        };

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Intent intent = new Intent(mContext, ReturnApplyDetailActivity.class);
                intent.putExtra("id", mDataList.get(position).getId());
                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        int height = DisplayUtil.getInstance(mContext).dp2px(14);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRvReturn.addItemDecoration(decoration);
        mRvReturn.setLayoutManager(new LinearLayoutManager(mContext));
        mRvReturn.setAdapter(adapter);
    }

    //    1. 新增退押申请；当用户贡献值达到所在等级退押所需的积分可向平台提出退还押金申请；
    //    2. 准代理无退还押金功能，达到退押条件，可自动转市级正式代理；
    @OnClick(R.id.btn_apply)
    void applyClick() {
        for (QuitDepositBean quitDepositBean : mDataList) {
            if (!quitDepositBean.getDone()) {
                ToastUtil.showToastInCenter(mContext, "已存在订单正在审核中，暂无法再次申请");
                return;
            }
        }
        if (applyDepositBean.getNeedContri() > applyDepositBean.getHaveContri()) {
            ToastUtil.showToastInCenter(mContext, "用户贡献值未达到所在等级退押所需的贡献值");
        } else {
            Intent intent = new Intent(mContext, ReturnApplyActivity.class);
            intent.putExtra("applyDepositBean", applyDepositBean);
            startActivityForResult(intent, 141);
        }
    }


    private void getQuitDeposit() {
        try {

            UpgradeBizFactory.createBiz().getQuitDeposit(this, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    if (data != null) {
                        mDataList.clear();
                        mDataList.addAll((List<QuitDepositBean>) data);
                        adapter.notifyDataSetChanged();
                    }

                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

    //Allen：申请退押金
    private void getApplyDeposit() {
        try {

            UpgradeBizFactory.createBiz().getApplyDeposit(this, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    applyDepositBean = (ApplyDepositBean) data;
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

    private void httpGetUserInfo() {
        try {
            UserBizFactory.createBiz().getUserInfo(this, new UICallBack() {
                @Override
                public void onStart() {

                }

                @Override
                public void onRefresh(Object data) {
                    UserBean bean = (UserBean) data;
                    if (data != null) {
                        mLoginUserBean = bean;
                    }
                }

                @Override
                public void onFailure(int status) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
